<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>$scope $watch 监听数据变化</title>
    <script src="../angular.js"></script>
</head>
<body>
    <div ng-app="gg" ng-controller="ctrl">
        标题： <input type="text" ng-model="news.title"> {{error}}
    </div>

<script>
    var mod = angular.module('gg', []);
    mod.controller('ctrl', ['$scope', function ($scope) {
//        $scope.title = '';
        $scope.news = {'title': ''};

        //在监听一个对象时，需要加上 true
        $scope.$watch('news', function (_new, old) {
           console.log(_new);
//           console.log(old);
            $scope.error = _new.title.length > 3 ? ' 标题长度不能超过3位' : '';

        },true)
    }])
</script>
</body>
</html>